﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity.Design.PluralizationServices;
using System.Globalization;

namespace DatabaseToCode.ExtensionMethods
{
    public static class StringExtensions
    {
        private static Lazy<PluralizationService> pluralizationService =
            new Lazy<PluralizationService>(() => PluralizationService.CreateService(CultureInfo.GetCultureInfo(1033)));

        /// <summary>
        /// Plurializes the specified value.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public static string Plurialize(this string value)
        {
            if (pluralizationService.Value.IsPlural(value))
            {
                return value;
            }
            return pluralizationService.Value.Pluralize(value);
        }

        /// <summary>
        /// Singuliarizes the specified value.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public static string Singuliarize(this string value)
        {
            if (pluralizationService.Value.IsSingular(value))
            {
                return value;
            }
            return pluralizationService.Value.Singularize(value);
        }

        /// <summary>
        /// Kamelizes the specified value.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public static string Kamelize(this string value) {
            return value.Substring(0, 1).ToLower() + value.Substring(1);
        }

        /// <summary>
        /// Safes the specified value.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public static string Safe(this string value) {
            return value.Replace(" ", string.Empty);
        }

        /// <summary>
        /// Safes the foreign key property.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public static string SafeForeignKeyProperty(this string value, string foreignProperty, string containingClass) {
            if (value.Equals(containingClass)) {
                value = string.Concat(foreignProperty, containingClass);
            }
            return value;
        }
    }

}
